<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="活动名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入活动名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动时间" prop="activeDate">
            <el-date-picker
              v-model="formData.activeDate"
              type="date"
              value-format="x"
              placeholder="选择活动时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动开始时间" prop="activeTimeBegin">
            <el-date-picker
              v-model="formData.activeTimeBegin"
              type="date"
              value-format="x"
              placeholder="选择活动开始时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动结束时间" prop="activeTimeEnd">
            <el-date-picker
              v-model="formData.activeTimeEnd"
              type="date"
              value-format="x"
              placeholder="选择活动结束时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="男士人数" prop="menNum">
            <el-input v-model="formData.menNum" placeholder="请输入男人数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="女士人数" prop="womenNum">
            <el-input v-model="formData.womenNum" placeholder="请输入女人数" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="负责人" prop="head">
            <el-input v-model="formData.head" placeholder="请输入负责人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="套餐类型" prop="packageType">
            <el-select v-model="formData.packageType" placeholder="请选择套餐类型">
              <el-option
                v-for="packageType in packageTypeList"
                :key="packageType.id"
                :label="packageType.name"
                :value="packageType.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="套餐名称" prop="packageName">
            <el-input v-model="formData.packageName" placeholder="请输入套餐名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动费用" prop="activeCost">
            <el-input v-model="formData.activeCost" placeholder="请输入活动费用" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止时间" prop="deadline">
            <el-date-picker
              v-model="formData.deadline"
              type="date"
              value-format="x"
              placeholder="选择活动截止时间"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动描述" prop="activeDesc">
            <el-input v-model="formData.activeDesc" placeholder="请输入活动描述" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="活动封面" prop="activeCover">
<!--            <el-image v-model="formData.activeCover" placeholder="请输入活动封面" />-->
            <UploadImg v-model="formData.activeCover"  placeholder=" 请上传活动封面" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审核时间" prop="deadline">
            <el-date-picker
              v-model="formData.auditTime"
              type="date"
              value-format="x"
              placeholder="选择活动截止时间"
            />
          </el-form-item>
        </el-col>

<!--        <el-col :span="12">-->
<!--          <el-form-item label="审核状态（0未审核 1已通过 2已驳回）" prop="auditStatus">-->
<!--            <el-radio-group v-model="formData.auditStatus">-->
<!--              <el-radio label="1">请选择字典生成</el-radio>-->
<!--            </el-radio-group>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="12">-->
<!--          <el-form-item label="审核人" prop="auditUser">-->
<!--            <el-input v-model="formData.auditUser" placeholder="请输入审核人" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as ActivityInfoApi from '@/api/blind/activityInfo'
import * as PackageTypeApi from '@/api/blind/packageType'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const packageTypeList = ref([]) // 套餐类型列表的数据
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: undefined,
  activeDate: undefined,
  activeTimeBegin: undefined,
  activeTimeEnd: undefined,
  menNum: undefined,
  womenNum: undefined,
  head: undefined,
  packageType: undefined,
  packageName: undefined,
  activeCost: undefined,
  deadline: undefined,
  activeDesc: undefined,
  activeCover: undefined,
  auditStatus: undefined,
  auditUser: undefined,
  auditTime: undefined,
  status: 1
})
const formRules = reactive({
  activeDate: [{ required: true, message: '活动时间不能为空', trigger: 'blur' }],
  deadline: [{ required: true, message: '截止时间不能为空', trigger: 'blur' }],
  activeCover: [{ required: true, message: '活动封面不能为空', trigger: 'blur' }],
  auditStatus: [{ required: true, message: '审核状态（0未审核 1已通过 2已驳回）不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const queryPackageParams = reactive({
  pageNo: 1,
  pageSize: 100,
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ActivityInfoApi.getActivityInfo(id)
    } finally {
      formLoading.value = false
    }
  }

  // 套餐类型
  const data = await PackageTypeApi.getPackageTypePage(queryPackageParams)
  packageTypeList.value = data.list
  // console.log("1111111----------------",data);
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ActivityInfoApi.ActivityInfoVO
    data.status = 1
    data.auditStatus =0
    if (formType.value === 'create') {
      await ActivityInfoApi.createActivityInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await ActivityInfoApi.updateActivityInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: undefined,
    activeDate: undefined,
    activeTimeBegin: undefined,
    activeTimeEnd: undefined,
    menNum: undefined,
    womenNum: undefined,
    head: undefined,
    packageType: undefined,
    packageName: undefined,
    activeCost: undefined,
    deadline: undefined,
    activeDesc: undefined,
    activeCover: undefined,
    auditStatus: undefined,
    auditUser: undefined
  }
  formRef.value?.resetFields()
}

</script>
